package com.fuershiye.woman.monitor.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.time.LocalDate;

import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Lists;

public class DateFormatUtil {

	private final static String REGEX_YEAR = "^[1,2](\\d{3})";
	
    public static final String YEAR = "yyyy";
    public static final String SHROT_DATE = "yyyy-MM-dd";
    public static final String SHROT_DATE_NUM = "yyyyMMdd";
    public static final String LONG_DATE = "yyyy-MM-dd HH:mm:ss";
    public static final String LONG_DATE_MM = "yyyy-MM-dd HH:mm";
    public static final String SHROT_DATE_SLASH = "yyyy/MM/dd";
    public static final String LONG_DATE_SLASH = "yyyy/MM/dd HH:mm:ss";
    public static final String SHROT_DATE_CHINA = "yyyy年MM月dd日";
    public static final String LONG_DATE_CHINA = "yyyy年MM月dd日 HH时mm分ss秒";

	// 用户未上报当年数据，次年的年初，时间默认展示上年数据，会有问题
	public static List<Integer> getHalfYears(){
		// 当前月份为【1-10月】
		int nowMonth = getNowMonth();
		if(nowMonth>=10 && nowMonth<=12){
			return getRangeYear(2020, getNowYear()-1);
		}else {
			return getRangeYear(2020, getNowYear()-2);
		}
	}

	public static List<Integer> getAreaYears(){
		return getRangeYear(2020, getNowYear()-1);
	}

	public static Integer getNowMonth() {
		LocalDate currentDate = LocalDate.now();
		// get the year
		int year = currentDate.getYear();
		// get the month
		int month = currentDate.getMonthValue();
		// get the day
		int day = currentDate.getDayOfMonth();
		return month;
	}

	public static Integer getNowYear() {
		Calendar be = Calendar.getInstance();
		return be.get(Calendar.YEAR);
	}

	public static Date getNowDateAfterMinute(int minute) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MINUTE, minute);
		return calendar.getTime();
	}

    public static Date stringToDate(String dateStr, String format) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    
    public static String dateToString(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
    
    public static String dateIntToStr(int before) {
    	
    	if(before<10000000 || before >= 100000000){
    		return "";
    	}
    	String str = before+"";
    	if(str.length()!=8){
    		return "";
    	}
    	return  str.substring(0,4)+"-" + str.substring(4,6)+"-" + str.substring(6,8);
    }


	public static List<Integer> getYears(Integer year) {

		List<Integer> result = Lists.newArrayList();

		if(isYear(year)){
			result.add(year);
		}

		return result;

	}
    
    /**
     * @param before	当前往前几年
     * @param after		当前往后几年
     * @return
     */
    public static List<Integer> getYears(Integer before, Integer after) {
    	
    	List<Integer> result = Lists.newArrayList();

    	//make 前几年
    	if(before>0){
        	Calendar be = Calendar.getInstance();
			be.set(Calendar.YEAR, be.get(Calendar.YEAR)-before);
			result.add(Integer.valueOf(getYear(be.getTime())));
    		
			for (int i=1;i<=before-1;i++) {
    			be.set(Calendar.YEAR, be.get(Calendar.YEAR)+1);
    			result.add(Integer.valueOf(getYear(be.getTime())));
    		}
    	}

    	//先赋值当年
    	Calendar c = Calendar.getInstance();
		Date stime = c.getTime();
		result.add(Integer.valueOf(getYear(stime)));
		
		//make 后几年
    	if(after>0){
        	Calendar af = Calendar.getInstance();
    		for (int i=0;i<after;i++) {
    			af.set(Calendar.YEAR, af.get(Calendar.YEAR)+1);
    			result.add(Integer.valueOf(getYear(af.getTime())));
    		}
    	}
    	
    	return result;
    }
    
    public static String getYear(Date time) {
    	
    	SimpleDateFormat sdf = new SimpleDateFormat(YEAR);
    	return sdf.format(time);
    }

	public static Integer getYear(String yearStr) {
    	if(!isYear(yearStr)){
			return null;
		}
		return Integer.parseInt(yearStr);
	}

    public static List<Integer> getRangeYear(Integer start, Integer end) {
    	
    	if(start==null||start==0){
        	Calendar c = Calendar.getInstance();
    		Date stime = c.getTime();
    		start = Integer.parseInt(getYear(stime)) - 5;
    	}
    	
    	if(end==null||end==0){
    		Calendar c = Calendar.getInstance();
    		Date stime = c.getTime();
    		end = Integer.parseInt(getYear(stime)) + 5;
    	}
    	
    	List<Integer> result = Lists.newArrayList();
    	int i = 0;
    	while(true){
    		if(start>=end){
    			break;
    		}
    		if(start+i>end){
    			break;
    		}
    		result.add(start+i);
    		i++;
    	}
    	return result;
    }
    
    /**
     * @param year 用英文逗号隔开的年份,例如:2011,2012,2013
     * @return
     */
    public static List<Integer> getYearStr2List(String year) {

		List<Integer> result = Lists.newArrayList();

    	if(StringUtils.isBlank(year)){
        	return result;
    	}
    	
    	String [] yearArray = year.trim().split(",");
		Set<Integer> yearList = Sets.newHashSet();
    	for (String str : yearArray) {
    		if(isYear(str)){
    			yearList.add(Integer.parseInt(str));
    		}
    	}

    	result.addAll(yearList);
    	return result;
    }
    
    /**
     * 判断是否是年
     * 
     * @param year
     * @return
     */
    public static boolean isYear(Integer year) {

    	if(year==null){
    		return false;
    	}
    	return isYear(year+"");
	}
    
    /**
     * 判断是否是年
     * 
     * @param year
     * @return
     */
    public static boolean isYear(String year) {
    	
    	if (StringUtils.isBlank(year)) {
    		return false;
    	}
    	
    	if(year.length()!=4){
    		return false;
    	}
    	
    	Pattern regular = Pattern.compile(REGEX_YEAR);
    	Matcher matcher = regular.matcher(year);
    	
    	return matcher.find();
    }
    
    public static void main(String[] args) {

//		System.out.println(getNowMonth());
		System.out.println(getHalfYears());
//		System.out.println(getAreaYears());

//
//		List<Integer> result = getTenYear(0,0);
//    	for (Integer string : result) {
//			System.out.println(string);
//		}
//    	List<Integer> result = getYears(1,1);
//    	for (Integer string : result) {
//    		System.out.println(string);
//    	}
    	
    	
//        System.out.println(DateFormatUtil.stringToDate("2009年11月11日 11时11分11秒", LONG_DATE_CHINA).toString());
//        System.out.println(DateFormatUtil.dateToString(new Date(), LONG_DATE_CHINA).toString());
    }
    
}